package com.example.demo.mapper;

import java.util.ArrayList;
import java.util.Date;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;

import com.example.demo.pojo.Cinema;
import com.woniuxy.movice.entity.Movie;
import com.woniuxy.movice.entity.MovieHall;
import com.example.demo.pojo.Round;
import com.example.demo.pojo.Round2;


public interface MovieMapper {
	//查询所有分院
	@Select("SELECT*FROM cinema")
	public ArrayList<Cinema> allCinema();
	//添加影厅
	@Insert("INSERT INTO movie_hall(ytid,yid,ytname,ytlayout) VALUES(default,#{yid},#{ytname},#{ytlayout})")
	public int addHall(MovieHall hall);
	@Select("SELECT COUNT(*) FROM movie_hall WHERE ytname=#{ytname}")
	public int sameName(MovieHall hall);

	//添加影片
	@Insert("INSERT INTO movie (mname,ref,mtype,mstar,mlength,mmassage,director,location, `language`)"
			+ " VALUES (#{mname},#{ref},#{mtype},#{mstar},#{mlength},#{mmassage},#{director},#{location},#{language})")
	public void addMovie(Movie movie);

	@Select("SELECT*FROM movie_hall WHERE yid=#{yid}")
	public ArrayList<MovieHall> allHall(int yid);
	@Select("SELECT id,mname,mlength FROM movie WHERE mshow=0")
	public ArrayList<com.example.demo.pojo.Movie> allMovie();
	
	@Select("SELECT round.id,round.mid,mname,round.yid,DATE_FORMAT(pstart, '%H:%i:%S') AS pstart,DATE_FORMAT(pend, '%H:%i:%S') AS pend,"
			+ "pprice,round.ytid,movie_hall.ytname,round.ytlayout FROM round LEFT JOIN movie "
			+ "ON round.mid=movie.id LEFT JOIN movie_hall ON round.ytid=movie_hall.ytid "
			+ "where DATE_FORMAT(pstart, '%Y-%m-%d')=#{vstart} AND round.yid=#{yid} AND round.ytid=#{ytid}"
	 + " ORDER BY pstart ASC" )
	public ArrayList<Round> showArranges(String vstart, int yid, int ytid);
	
	@Select("SELECT ytlayout FROM movie_hall WHERE ytid=#{ytid}")
	public String findLayout(Integer ytid);
	
	@Select("SELECT COUNT(*) FROM round WHERE (pstart>=#{pstart} AND pstart<=#{pend})" + 
			"OR(pstart<=#{pstart} AND pend>=#{pend})OR(pend>=#{pstart} AND pend<=#{pend})")
	public int interTime(Date pstart, Date pend);
	
	@Insert("INSERT INTO round VALUES(default,#{mid},#{yid},#{pstart},#{pend},#{pprice},#{ytid},#{ytlayout})")
	public void addArrange(Round2 round);
	
	@Select("SELECT COUNT(*) FROM orders WHERE cid=#{id}")
	public int findOrderCount(int id);
	
	@Delete("DELETE FROM round WHERE id=#{id}")
	public void deleArrange(int id);
	
	@Select("SELECT COUNT(*) FROM round WHERE ytid=#{ytid} AND pend > NOW()")
	public int roundByYt(int ytid);
	
	@Delete("DELETE FROM movie_hall WHERE ytid=#{ytid}")
	public void deleHall(int ytid);

}
